<?php

namespace App\Http\Controllers;

use App\Common\Constant\Status;
use App\Services\ICookieService;
use Illuminate\Http\Request;
use App\Services\IWeixinService;
use App\Services\IUserService;
use App\Services\ISessionService;
use Log;

/**
 * 微信
 * Class WeixinController
 * @package App\Http\Controllers
 */
class WeixinController extends BaseController
{
    
    /**
     * 用户授权,获取企业号用户
     * @param Request $request
     * @param ISessionService $sessionService
     * @return type
     */
    public function getWeixinUser(Request $request,IWeixinService $weixinService,ISessionService $sessionService,ICookieService $cookieService){
        /**
         * 检查微信用户
         */
        $weixinUser = $sessionService->getCurrentWeixinUser();
        if (empty($weixinUser)){
            $token = $sessionService->getFlashToken();//用于企业或服务商自行校验session，防止跨域攻击
            if (!$token){
                $token = $sessionService->genFlashToken();
                if (!$token){
                    return $this->renderFail(Status::WEIXIN_TOKEN_ERROR);
                }
                return $weixinService->getUserAuthorize(url('weixin/getweixinuser'),$token);
            }else if ($request->input('state') != $token || !$request->has('code')){
                return $this->renderFail(Status::WEIXIN_AUTH_WRONG);
            }

            $code = $request->input('code');
            $weixinUser = $weixinService->getUser($code);
            if (empty($weixinUser->UserId) && empty($weixinUser->OpenId)){
                Log::error(status(Status::WEIXIN_API_RETURN).' code: '.$code.' result: '.json_encode($weixinUser));
                return $this->renderFail(Status::WEIXIN_API_RETURN);
            }
            if (empty($weixinUser->UserId)){
                Log::error(status(Status::WEIXIN_NOT_MEMBER).' code: '.$code.' result: '.json_encode($weixinUser));
                return $this->renderFail(Status::WEIXIN_NOT_MEMBER);
            }
            if (!$sessionService->setCurrentWeixinUser($weixinUser)){
                return $this->renderFail(Status::SESSION_SETTING_ERROR);
            }
        }

        /**
         * cookie存储当前微信用户id
         */
        if (!$cookieService->currentWeixinUserid($weixinUser->UserId)){
            return $this->renderFail(Status::COOKIE_SETTING_ERROR);
        }
        return redirect($sessionService->getRedirectUrl('autologin'));
    }
    
    /**
     * 用户授权,获取企业号用户信息
     * @param Request $request
     * @param ISessionService $sessionService
     * @return type
     */
    public function getWeixinUserinfo(Request $request,IWeixinService $weixinService,ISessionService $sessionService,ICookieService $cookieService){
        /**
         * 检查微信用户
         */
        $weixinUserinfo = $sessionService->getCurrentWeixinUserinfo();
        if (empty($weixinUser)){

        }

        $token = $sessionService->getFlashToken();//用于企业或服务商自行校验session，防止跨域攻击
        if (!$token){
            $token = $sessionService->genFlashToken();
            if (!$token){
                return $this->renderFail(Status::WEIXIN_TOKEN_ERROR);
            }
            return $weixinService->getUserinfoAuthorize(url('weixin/getweixinuserinfo'),$token);
        }else if ($request->input('state') != $token || !$request->has('auth_code')){
            return $this->renderFail(Status::WEIXIN_LOGIN_WRONG);
        }

        $auth_code = $request->input('auth_code');
        $weixinUserinfo = $weixinService->getUserinfo($auth_code);
        if (empty($weixinUserinfo->usertype) && empty($weixinUserinfo->user_info)){
            return $this->renderFail(Status::WEIXIN_API_RETURN);
        }
        if (empty($weixinUserinfo->user_info->userid)){
            return $this->renderFail(Status::WEIXIN_NOT_USER);
        }
        if (!$sessionService->setCurrentWeixinUserinfo($weixinUserinfo)){
            return $this->renderFail(Status::SESSION_SETTING_ERROR);
        }
        return redirect($sessionService->getRedirectUrl('autologin'));
    }
    
}
